package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DoseMenu;
import com.ruoyi.system.domain.ProjectCheckDetail;
import com.ruoyi.system.domain.res.doseMenuDetail.DayMenuConfirm_res;
import com.ruoyi.system.domain.res.doseMenuDetail.DayMenuLog_res;
import com.ruoyi.system.mapper.DoseMenuMapper;
import com.ruoyi.system.mapper.ProjectCheckDetailMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DoseMenuLogMapper;
import com.ruoyi.system.domain.DoseMenuLog;
import com.ruoyi.system.service.IDoseMenuLogService;

import javax.annotation.Resource;

/**
 * 每周菜单确认记录Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Service
public class DoseMenuLogServiceImpl implements IDoseMenuLogService {
    private static final Logger log = LoggerFactory.getLogger(DoseMenuLogServiceImpl.class);
    @Resource
    private DoseMenuLogMapper doseMenuLogMapper;
    @Resource
    private DoseMenuMapper doseMenuMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private ProjectCheckDetailMapper projectCheckDetailMapper;

    /**
     * 查询每周菜单确认记录
     *
     * @param id 每周菜单确认记录主键
     * @return 每周菜单确认记录
     */
    @Override
    public DoseMenuLog selectDoseMenuLogById(Long id) {
        return doseMenuLogMapper.selectDoseMenuLogById(id);
    }

    /**
     * 查询每周菜单确认记录列表
     *
     * @param doseMenuLog 每周菜单确认记录
     * @return 每周菜单确认记录
     */
    @Override
    public List<DoseMenuLog> selectDoseMenuLogList(DoseMenuLog doseMenuLog) {
        return doseMenuLogMapper.selectDoseMenuLogList(doseMenuLog).stream().peek(item -> {
            SysUser sysUser = sysUserMapper.selectUserById(item.getUserId());
            if (sysUser != null) {
                item.setNickName(sysUser.getNickName());
            }
        }).collect(Collectors.toList());
    }

    /**
     * 菜谱确认统计
     */
    @Override
    public List<DayMenuLog_res> selectDayMenuLog(DoseMenuLog doseMenuLog) {
        List<DayMenuLog_res> resList = new ArrayList<>();
        //查询在当前选择时间区间的每周菜单记录
        List<DoseMenu> doseMenus = doseMenuMapper.selectDoseMenuList(new DoseMenu() {{
            setWeekSearch(doseMenuLog.getWeek());
        }});
        if (doseMenus == null || doseMenus.isEmpty()) {
            log.info("每周菜单记录为空");
            resList.add(new DayMenuLog_res(2, BigDecimal.ZERO));
            resList.add(new DayMenuLog_res(3, BigDecimal.ZERO));
            resList.add(new DayMenuLog_res(4, BigDecimal.ZERO));
            return resList;
        }
        List<DoseMenuLog> doseMenuLogs = doseMenuLogMapper.selectDoseMenuLogList(new DoseMenuLog() {{
            setDoseId(doseMenus.get(0).getId());
        }});
        if (doseMenuLogs == null || doseMenuLogs.isEmpty()) {
            log.info("每周菜单确认记录为空");
            resList.add(new DayMenuLog_res(2, BigDecimal.ZERO));
            resList.add(new DayMenuLog_res(3, BigDecimal.ZERO));
            resList.add(new DayMenuLog_res(4, BigDecimal.ZERO));
            return resList;
        }
        long count_type2 = doseMenuLogs.stream().filter(item -> item.getType() == 2).count();
        long count_type3 = doseMenuLogs.stream().filter(item -> item.getType() == 3).count();
        long count_type4 = doseMenuLogs.stream().filter(item -> item.getType() == 4).count();
        resList.add(new DayMenuLog_res(2, BigDecimal.valueOf(count_type2).divide(BigDecimal.valueOf(doseMenuLogs.size()), 4, RoundingMode.HALF_UP)));
        resList.add(new DayMenuLog_res(3, BigDecimal.valueOf(count_type3).divide(BigDecimal.valueOf(doseMenuLogs.size()), 4, RoundingMode.HALF_UP)));
        resList.add(new DayMenuLog_res(4, BigDecimal.valueOf(count_type4).divide(BigDecimal.valueOf(doseMenuLogs.size()), 4, RoundingMode.HALF_UP)));
        return resList;
    }

    /**
     * 目测检查统计
     */
    @Override
    public List<DayMenuConfirm_res> selectDayMenuConfirm(DoseMenuLog doseMenuLog) {
        List<DayMenuConfirm_res> resList = new ArrayList<>();
        //根据日期查询对应配送单关联的所有检查记录
        List<ProjectCheckDetail> checkDetails = projectCheckDetailMapper.selectProjectCheckDetailByWeek(doseMenuLog.getWeek());
        if (checkDetails == null || checkDetails.isEmpty()) {
            resList.add(new DayMenuConfirm_res(0, BigDecimal.ZERO));
            resList.add(new DayMenuConfirm_res(1, BigDecimal.ZERO));
            resList.add(new DayMenuConfirm_res(2, BigDecimal.ZERO));
            return resList;
        }
        long count_status0 = checkDetails.stream().filter(item -> item.getStatus() == 0).count();
        long count_status1 = checkDetails.stream().filter(item -> item.getStatus() == 1).count();
        long count_status2 = checkDetails.stream().filter(item -> item.getStatus() == 2).count();
        resList.add(new DayMenuConfirm_res(0, BigDecimal.valueOf(count_status0).divide(BigDecimal.valueOf(checkDetails.size()), 4, RoundingMode.HALF_UP)));
        resList.add(new DayMenuConfirm_res(1, BigDecimal.valueOf(count_status1).divide(BigDecimal.valueOf(checkDetails.size()), 4, RoundingMode.HALF_UP)));
        resList.add(new DayMenuConfirm_res(2, BigDecimal.valueOf(count_status2).divide(BigDecimal.valueOf(checkDetails.size()), 4, RoundingMode.HALF_UP)));
        return resList;
    }

    /**
     * 新增每周菜单确认记录
     *
     * @param doseMenuLog 每周菜单确认记录
     * @return 结果
     */
    @Override
    public int insertDoseMenuLog(DoseMenuLog doseMenuLog) {
        DoseMenu doseMenu = doseMenuMapper.selectDoseMenuById(doseMenuLog.getDoseId());
        if (doseMenu == null) {
            throw new ServiceException("未获取到每周菜单信息");
        }
        if (doseMenu.getDeadLine() != null) {
            if (doseMenu.getDeadLine().getTime() <= System.currentTimeMillis()) {
                throw new ServiceException("已超过确认期限，截止日期【" + DateUtil.format(doseMenu.getDeadLine(), "yyyy-MM-dd HH:mm:ss") + "】");
            }
        }
        doseMenuLog.setCreateTime(DateUtils.getNowDate());
        //查询是否确认过
        List<DoseMenuLog> list = doseMenuLogMapper.selectDoseMenuLogList(new DoseMenuLog(doseMenuLog.getDoseId(), doseMenuLog.getUserId(), doseMenuLog.getType()));
        if (list != null && !list.isEmpty()) {
            throw new ServiceException("已经操作过，无需重复操作");
        }
        return doseMenuLogMapper.insertDoseMenuLog(doseMenuLog);
    }

    /**
     * 修改每周菜单确认记录
     *
     * @param doseMenuLog 每周菜单确认记录
     * @return 结果
     */
    @Override
    public int updateDoseMenuLog(DoseMenuLog doseMenuLog) {
        doseMenuLog.setUpdateTime(DateUtils.getNowDate());
        return doseMenuLogMapper.updateDoseMenuLog(doseMenuLog);
    }

    /**
     * 批量删除每周菜单确认记录
     *
     * @param ids 需要删除的每周菜单确认记录主键
     * @return 结果
     */
    @Override
    public int deleteDoseMenuLogByIds(Long[] ids) {
        return doseMenuLogMapper.deleteDoseMenuLogByIds(ids);
    }

    /**
     * 删除每周菜单确认记录信息
     *
     * @param id 每周菜单确认记录主键
     * @return 结果
     */
    @Override
    public int deleteDoseMenuLogById(Long id) {
        return doseMenuLogMapper.deleteDoseMenuLogById(id);
    }
}
